#include "CF_Core.h"

CCF_Log* CCF_Engine::s_log = NULL;
CCF_Settings* CCF_Engine::s_settings = NULL;

CCF_Engine::CCF_Engine(HINSTANCE instance)
{
  this->m_rootWindow = new CCF_Window(instance);
  this->m_camera = new CCF_Camera();
  this->m_drawRenderer = new CCF_DrawRenderer();
  this->m_interface = new CCF_Interface();
}

CCF_Engine::~CCF_Engine(void)
{
}

void CCF_Engine::Run()
{
  this->OnInit();
  this->m_rootWindow->Show();

  double nowTicks, currentTicks = GetTickCount();

  while(!m_rootWindow->IsDestroy())
  {
    this->m_drawRenderer->Begin();
    this->m_drawRenderer->SetCamera(this->m_camera);

    nowTicks = GetTickCount();
    this->OnTick( (nowTicks - currentTicks)/100 );
    currentTicks = nowTicks;

    this->m_interface->Render();
    this->m_drawRenderer->End();
  }

  this->OnExit();
}

void CCF_Engine::OnInit()
{
  m_rootWindow->Initialize();
  m_drawRenderer->Initialize(m_rootWindow->GetHandle());
  m_interface->Initialize(m_drawRenderer->GetDevice());
}

void CCF_Engine::OnTick( double delta )
{
}

void CCF_Engine::OnExit()
{

}

CCF_Log* CCF_Engine::GetLog()
{
  if( s_log == NULL )
  {
    s_log = new CCF_Log();
    s_log->BeginLogging(CR_DEFAULT_LOG_FILE);
  }

  return s_log;
}

CCF_Settings* CCF_Engine::GetSettings()
{
  if( s_settings == NULL )
  {
    s_settings = new CCF_Settings();
    s_settings->loadFromCfg( CR_DEFAULT_SETTINGS_FILE );
  }

  return s_settings;
}